﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace OnlineDovizOtomasyonu
{
    public partial class musteriduzenle : System.Web.UI.Page
    {
        Fonksiyon Sistem = new Fonksiyon();
        Mesaj Mesaj = new Mesaj();
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Page.IsPostBack == false)
            //{
            //CariNumara();
            SehirCek();
            //}
        }

        public void messagebox(string mesaj)
        {
            Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('" + mesaj + "')</SCRIPT>");
        }

        void CariSil(int MusetiID)
        {
            SqlConnection baglanti = Sistem.baglan();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = baglanti;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_WEBINGDOVIZ_CARI_SIL";
            cmd.Parameters.Add("ID", txtMusteriKoduSec.Value);
            cmd.ExecuteNonQuery();
        }

        void CariGetir(int MusteriID)
        {
            SqlConnection con = Sistem.baglan();
            SqlCommand komut = new SqlCommand();
            komut.Connection = con;
            komut.CommandText = "Select * From WEBINGDOVIZ_TBLCARI WHERE ID=" + MusteriID;
            SqlDataReader dr = komut.ExecuteReader();
            while (dr.Read())
            {
                txtMusteriAdi.Text = dr["ADI"].ToString();
                txtMusteriGroup.Value = dr["GRUP"].ToString();
                txtDurum.Value = dr["DURUM"].ToString();
                txtAdres.Text = dr["ADRES"].ToString();
                txtSehir.Text = dr["IL"].ToString();
                txtIlce.Text = dr["ILCE"].ToString();
                txtPostaKodu.Text = dr["POSTAKODU"].ToString();
                txtWebAdresi.Text = dr["WEB"].ToString();
                txtMailAdresi.Text = dr["MAIL"].ToString();
                txtTel1.Text = dr["TEL1"].ToString();
                txtTel2.Text = dr["TEL2"].ToString();
                txtCep1.Text = dr["CEP1"].ToString();
                txtCep2.Text = dr["CEP2"].ToString();
                txtFaks.Text = dr["FAKS"].ToString();
                txtTC.Text = dr["TC"].ToString();
                txtVergiNo.Text = dr["VERGINO"].ToString();
                txtVergiDairesi.Text = dr["VERGIDAIRESI"].ToString();
                txtSube1.Text = dr["SUBE1"].ToString();
                txtBanka1.Text = dr["BANKA1"].ToString();
                txtHesapNo1.Text = dr["HESAP1"].ToString();
                txtIban1.Text = dr["IBAN1"].ToString();
                txtSube2.Text = dr["SUBE2"].ToString();
                txtBanka2.Text = dr["BANKA2"].ToString();
                txtHesapNo2.Text = dr["HESAP2"].ToString();
                txtIban2.Text = dr["IBAN2"].ToString();
                txtSube3.Text = dr["SUBE3"].ToString();
                txtBanka3.Text = dr["BANKA3"].ToString();
                txtHesapNo3.Text = dr["HESAP3"].ToString();
                txtIban3.Text = dr["IBAN3"].ToString();
                txtSube4.Text = dr["SUBE4"].ToString();
                txtBanka4.Text = dr["BANKA4"].ToString();
                txtHesapNo4.Text = dr["HESAP4"].ToString();
                txtIban4.Text = dr["IBAN4"].ToString();
            }
        }

        void SehirCek()
        {
            txtSehir.Items.Clear();
            DataTable Sehirler = Sistem.GetDataTable("Select * From TBLIL ORDER BY ID");
            for (int i = 0; i < Sehirler.Rows.Count; i++)
            {
                DataRow Sehir = Sehirler.Rows[i];
                txtSehir.Items.Add(Sehir["SEHIR"].ToString());
                txtSehir.Items[i].Value = Sehir["KOD"].ToString();
            }
        }

        void IlceCek(int Sehir)
        {
            txtIlce.Items.Clear();
            DataTable Ilceler = Sistem.GetDataTable("Select * From TBLILCE WHERE SEHIR=" + Sehir + " ORDER BY ID");
            for (int i = 0; i < Ilceler.Rows.Count; i++)
            {
                DataRow Ilce = Ilceler.Rows[i];
                txtIlce.Items.Add(Ilce["ILCE"].ToString());
                txtIlce.Items[i].Value = Ilce["ID"].ToString();
            }
        }

        void GroupCek()
        {
            txtMusteriGroup.Items.Clear();
            DataTable Sehirler = Sistem.GetDataTable("Select * From WEBINGDOVIZ_TBLGROUP ORDER BY ID");
            for (int i = 0; i < Sehirler.Rows.Count; i++)
            {
                DataRow Sehir = Sehirler.Rows[i];
                txtMusteriGroup.Items.Add(Sehir["ADI"].ToString());
                txtMusteriGroup.Items[i].Value = Sehir["ID"].ToString();
            }
        }

        protected void txtIl_SelectedIndexChanged(object sender, EventArgs e)
        {
            IlceCek(Convert.ToInt32(txtSehir.Value));
        }

        protected void btnKaydet_Click(object sender, EventArgs e)
        {

            Kaydet();
            Response.Redirect("musteriduzenle.aspx");
          
        }

        protected void btnTemizle_Click(object sender, EventArgs e)
        {
            Response.Redirect("musteriduzenle.aspx");
        }

        void Kaydet()
        {
            SqlConnection baglanti = Sistem.baglan();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = baglanti;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_WEBINGDOVIZ_CARI_DUZENLE";
            cmd.Parameters.Add("ID", txtMusteriKoduSec.Value);
            cmd.Parameters.Add("KODU", txtMusteriKoduSec.Text);
            cmd.Parameters.Add("ADI", txtMusteriAdi.Text);
            cmd.Parameters.Add("GRUP", txtMusteriGroup.Value);
            cmd.Parameters.Add("DURUM", txtDurum.Value);
            cmd.Parameters.Add("ADRES", txtAdres.Text);
            cmd.Parameters.Add("IL", txtSehir.Text);
            cmd.Parameters.Add("ILCE", txtIlce.Text);
            cmd.Parameters.Add("POSTAKODU", txtPostaKodu.Text);
            cmd.Parameters.Add("WEB", txtWebAdresi.Text);
            cmd.Parameters.Add("MAIL", txtMailAdresi.Text);
            cmd.Parameters.Add("TEL1", txtTel1.Text);
            cmd.Parameters.Add("TEL2", txtTel2.Text);
            cmd.Parameters.Add("CEP1", txtCep1.Text);
            cmd.Parameters.Add("CEP2", txtCep2.Text);
            cmd.Parameters.Add("FAKS", txtFaks.Text);
            cmd.Parameters.Add("TC", txtTC.Text);
            cmd.Parameters.Add("VERGINO", txtVergiNo.Text);
            cmd.Parameters.Add("VERGIDAIRESI", txtVergiDairesi.Text);
            cmd.Parameters.Add("SUBE1", txtSube1.Text);
            cmd.Parameters.Add("BANKA1", txtBanka1.Text);
            cmd.Parameters.Add("HESAP1", txtHesapNo1.Text);
            cmd.Parameters.Add("IBAN1", txtIban1.Text);
            cmd.Parameters.Add("SUBE2", txtSube2.Text);
            cmd.Parameters.Add("BANKA2", txtBanka2.Text);
            cmd.Parameters.Add("HESAP2", txtHesapNo2.Text);
            cmd.Parameters.Add("IBAN2", txtIban2.Text);
            cmd.Parameters.Add("SUBE3", txtSube3.Text);
            cmd.Parameters.Add("BANKA3", txtBanka3.Text);
            cmd.Parameters.Add("HESAP3", txtHesapNo3.Text);
            cmd.Parameters.Add("IBAN3", txtIban3.Text);
            cmd.Parameters.Add("SUBE4", txtSube4.Text);
            cmd.Parameters.Add("BANKA4", txtBanka4.Text);
            cmd.Parameters.Add("HESAP4", txtHesapNo4.Text);
            cmd.Parameters.Add("IBAN4", txtIban4.Text);
            cmd.ExecuteNonQuery();
            //Response.Redirect("musterigrouptanimla.aspx");
        }

        protected void txtMusteriKoduSec_SelectedIndexChanged(object sender, EventArgs e)
        {
            CariGetir(Convert.ToInt32(txtMusteriKoduSec.Value));
        }

        protected void btnSil_Click(object sender, EventArgs e)
        {
            CariSil(Convert.ToInt32(txtMusteriKoduSec.Value));
            Response.Redirect("musteriduzenle.aspx");
        }
    }
}